import * as mars3d from "mars3d";
import * as Cesium from "mars3d-cesium";
import "@/utils/kml-geojson.js";
import "@/utils/Kml2JsonLayer.js";
import mapConfig from "./mapConfig";
import { color } from "echarts";
interface initMap {
    map: any;
    graphicLayer: any;
    graphic: any;
}
class initMap {
    constructor() {
        this.map;
        this.graphicLayer;
        this.graphic;
    }
    init(id: string, initcallback: Function) {
        // 创建三维地球场景
        this.map = new mars3d.Map(id, mapConfig);
        console.log("地图加载完成^_^");
        this.graphicLayer = new mars3d.layer.GraphicLayer({
            zIndex: 999,
        });
        this.map.addLayer(this.graphicLayer);
        // this.setTif(this.map);
        initcallback(this.map);
    }
    removeLayer() {
        this.graphicLayer.clear();
    }
    // 运动轨迹
    setTrack(data: any, callback: Function) {
        if (this.graphic) {
            // console.log(data, "坐标");
            this.graphic.setCallbackPositions(data);
            // this.graphic.setOptions({ positions: data });
        } else {
            this.removeLayer();
            this.graphic = new mars3d.graphic.PolylineEntity({
                positions: data,
                style: {
                    width: 2,
                    clampToGround: true,
                    color: Cesium.Color.YELLOW,
                    closure: false,
                },
            });
            this.graphicLayer.addGraphic(this.graphic);

            setTimeout(() => {
                callback(this.graphic.distance);
            }, 500);
            this.map.flyToGraphic(this.graphicLayer, {
                radius: 5000,
            });
        }
    }
    // 绘制分区标绘
    setSubarea(data: any, isRemove: boolean = true) {
        // return;
        if (isRemove) {
            this.removeLayer();
        }
        this.graphic = undefined;
        data.forEach((v: any) => {
            // 需要显示的数据
            if (v.isShow == "1") {
                // 多边形
                if (v.type == "polygon") {
                    let styles = JSON.parse(v.style);
                    let polygon: any = new mars3d.graphic.PolygonEntity({
                        positions: JSON.parse(v.positions),
                        style: styles,
                    });
                    polygon.deviceId = v.deviceId;
                    polygon.setStyle({
                        outlineWidth: 2,
                        label: {
                            text: v.name,
                            opacity: 1,
                            scaleByDistance: true,
                        },
                        outlineColor: "#000000",
                        highlight: {
                            outlineColor: "#0DFFF0",
                            label: {
                                color: "#0DFFF0",
                                outlineColor: "#0DFFF0",
                                font_size: 28,
                            },
                        },
                    });
                    this.graphicLayer.addGraphic(polygon);
                } else if (v.type == "polyline") {
                    // 线段
                    let styles = JSON.parse(v.style);
                    let polyline = new mars3d.graphic.PolylineEntity({
                        positions: JSON.parse(v.positions),
                        style: styles,
                    });
                    polyline.setStyle({ label: { text: "" }, zIndex: 0 });
                    setTimeout(() => {
                        this.graphicLayer.addGraphic(polyline);
                    }, 100);
                }
            }
        });
        this.map.flyToGraphic(this.graphicLayer, {
            radius: 3000,
        });
    }
    setTif(map: any) {
        // // 202311双龙数字果园tif瓦片
        const tileLayer = new mars3d.layer.XyzLayer({
            // url: "https://scwff.com/weChatMiniProgramIcon/shuanglong/{z}/{x}/{y}.png",
            url: "http://59.110.54.18:7002/{z}/{x}/{y}.png",
            minimumLevel: 14,
            maximumLevel: 20,
            rectangle: { xmin: 108.6035815096, xmax: 108.6254601178, ymin: 31.2009061983, ymax: 31.2256796335 },
            flyTo: true,
        });
        map.addLayer(tileLayer);
    }
}
const maps = new initMap();
// maps.setTif();
export default maps;
